package com.systemic.service.impl;

import com.systemic.mapper.*;
import com.systemic.pojo.*;
import com.systemic.service.GroupRelationService;
import com.systemic.vo.PageVo;
import com.systemic.vo.SearchUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: GroupRelationServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-02 14:57
 * @Version:
 **/
@Service
public class GroupRelationServiceImpl implements GroupRelationService {

    @Resource
    private GroupRelationMapper groupRelationMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private GroupInfoMapper groupInfoMapper;

    @Resource
    private GroupMindfulnessInfoMapper groupMindfulnessInfoMapper;

    @Resource
    private GroupMindfulnessMapper groupMindfulnessMapper;

    /**
     * 查看已加入的团体
     *
     * @param pageVo
     * @return
     */
    @Override
    public List<GroupInfo> relationJoin(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        Integer uid = pageVo.getUid();
        // 查询用户加入了哪些团体
        List<GroupRelation> groupRelationList = groupRelationMapper.selectListByUid(uid,offset,pageSize);
        List<GroupInfo> groupInfoList = new ArrayList<>();
        for (GroupRelation groupR:groupRelationList) {
            GroupInfo info = new GroupInfo();
            info.setId(groupR.getGroupId());
            GroupInfo res = groupInfoMapper.selectONE(info);
            groupInfoList.add(res);
        }
        return groupInfoList;
    }

    /**
     * 成员列表
     *
     * @param pageVo
     * @return
     */
    @Override
    public List<SearchUser> relationList(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        Integer groupId = pageVo.getId();
        List<GroupRelation> groupRelationList = groupRelationMapper.selectListByGid(groupId,offset,pageSize);
        List<SearchUser> res = new ArrayList<>();
        if (!groupRelationList.isEmpty()){
            for (GroupRelation groupR:groupRelationList) {
                UserInfo info = userInfoMapper.selectUserInfoByUid(groupR.getUid());
                SearchUser user = new SearchUser();
                user.setUid(info.getUid());
                user.setGender(info.getGender());
                user.setUname(info.getUname());
                user.setAvatar(info.getAvatar());
                res.add(user);
            }
        }
        return res;
    }

    /**
     * 退出团体
     *
     * @param groupRelation
     * @return
     */
    @Override
    @Transactional
    public int relationDelete(GroupRelation groupRelation) {
        int row = 0;
        Integer promoter = groupRelation.getUid();
        GroupMindfulnessInfo groupMindfulnessInfo = new GroupMindfulnessInfo();
        groupMindfulnessInfo.setPromoter(promoter);
        List<GroupMindfulnessInfo> infoList = groupMindfulnessInfoMapper.select(groupMindfulnessInfo);
        if (!infoList.isEmpty()){
            for (GroupMindfulnessInfo info:infoList) {
                GroupMindfulness groupMindfulness = new GroupMindfulness();
                groupMindfulness.setGmId(info.getId());
                row = groupMindfulnessMapper.delete(groupMindfulness) + row;     // 删除成员发起的活动关系表
                row = groupMindfulnessInfoMapper.delete(info) + row;   // 删除成员发起的活动
            }
        }
        GroupRelation gro = new GroupRelation();
        gro.setUid(promoter);
        gro.setGroupId(groupRelation.getGroupId());
        row = groupRelationMapper.delete(gro)+row;
        return row;
    }
}
